package com.yand.disk.demo.controller;


import com.yand.disk.demo.config.FileInfoConfig;
import com.yand.disk.demo.entity.FileInfoBean;
import com.yand.disk.demo.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


/**
 * @author yanzz
 *  下载控制器类
 *
 */
@Controller
public class NetDiskController {



    @Autowired
    private RedisUtil redisUtil;


    /**
     * 跳转网盘管理页面
     * @return
     */
    @GetMapping("/init")
    public ModelAndView initPage(){

        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("/index");

        return modelAndView;

    }


    /**
     * 下载文件示例代码
     * @param fileId
     * @param response
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/downLoadFile")
    public void   downLoadFile(HttpServletResponse response , HttpServletRequest request, Model model) {

        /**
         * 这边查询到具体的某一个文件
         *  正常根据fileId从数据库中读取，这边就从配置文件中获取
         */
        FileInfoBean fileInfoBean = FileInfoConfig.fileInfoBean;

        //待下载文件名
        String fileName = "1.png";
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        byte[] buff = new byte[1024];
        //创建缓冲输入流
        BufferedInputStream bis = null;
        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();

            //这个路径为待下载文件的路径
            bis = new BufferedInputStream(new FileInputStream(new File(fileInfoBean.getFilePath())));
            int read = bis.read(buff);

            //通过while循环写入到指定了的文件夹中
            while (read != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                read = bis.read(buff);
            }

        } catch ( IOException e ) {
            e.printStackTrace();
            //出现异常返回给页面失败的信息
            model.addAttribute("result","下载失败");
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //最后给下载的次数累计+1 admin 为当前登录人
            redisUtil.incr("fileDownLoadCount:"+"admin:"+fileInfoBean.getId());
        }

    }

    /**
     * 跳转下载文件列表页面
     * @param fileuuid
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("s/{fileuuid}")
    public ModelAndView toList(@PathVariable(value = "fileuuid") String fileuuid,HttpServletRequest request, Model model){

        /**
         * 这边从fileuuid中获取关联的文件
         * 1，生成下载的token值，存放在下载的下载历史表中
         *
         */
        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("/list");

        return modelAndView;

    }







    /**
     * 跳转输入提取码页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("share/init")
    public ModelAndView toVerificatePage(HttpServletRequest request, Model model){

        String fileId = request.getParameter("surl");
        model.addAttribute("surl", fileId);

        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("/verificate");

        return modelAndView;

    }

    /**
     * 跳转失效页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("expire")
    public ModelAndView toExpirePage(HttpServletRequest request, Model model){

        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("expire");

        return modelAndView;

    }

    /**
     * 跳转超过下载次数页面
     * @param request
     * @param model
     * @return
     */
    @GetMapping("count")
    public ModelAndView toCountPage(HttpServletRequest request, Model model){

        ModelAndView modelAndView = new  ModelAndView();
        modelAndView.setViewName("/count");

        return modelAndView;

    }










}
